package com.llgz.hotelmanageapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.llgz.hotelmanageapi.common.CommonResult;
import com.llgz.hotelmanageapi.mapper.FinancialBillMapper;
import com.llgz.hotelmanageapi.pojo.DO.CheckinInfo;
import com.llgz.hotelmanageapi.pojo.DO.CheckoutInfo;
import com.llgz.hotelmanageapi.mapper.CheckoutInfoMapper;
import com.llgz.hotelmanageapi.pojo.DO.ConsumptionInfo;
import com.llgz.hotelmanageapi.pojo.DO.FinancialBill;
import com.llgz.hotelmanageapi.pojo.DO.OrderInfo;
import com.llgz.hotelmanageapi.service.CheckoutInfoServer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llgz.hotelmanageapi.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.sl.draw.geom.QuadToCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.InfoProperties;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 退房登记信息 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@Service
public class CheckoutInfoServiceImpl extends ServiceImpl<CheckoutInfoMapper, CheckoutInfo> implements CheckoutInfoServer {

    @Autowired
    private CheckoutInfoMapper checkoutInfoMapper;

    @Autowired
    private FinancialBillMapper financialBillMapper;

    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    @Autowired
    private CheckinInfoServiceImpl checkinInfoService;

    @Autowired
    private ConsumptionInfoServiceImpl consumptionInfoService;

    @Autowired
    private RoomInfoServiceImpl roomInfoService;

    public CommonResult getAllUncheckOut(String name, Integer currentPage, Integer size) {
        currentPage = (currentPage - 1) * size;
        if (!StringUtils.isEmpty(name)){
            List<FinancialBill> allUncheckOut = checkoutInfoMapper.getAllUncheckOut(name,currentPage, size);
            Integer total = checkoutInfoMapper.getAllUncheckOutCount(name);
            Map<String,Object> map = new HashMap<>();
            map.put("total", total);
            map.put("data",allUncheckOut);
            return CommonResult.successResponse(map);
        }
        List<FinancialBill> allUncheckOut = checkoutInfoMapper.getAllUncheckOut(null,currentPage, size);
        Integer total = checkoutInfoMapper.getAllUncheckOutCount(null);
        Map<String,Object> map = new HashMap<>();
        map.put("total", total);
        map.put("data",allUncheckOut);
        return CommonResult.successResponse(map);
    }

    public CommonResult addFinancialBill(FinancialBill financialBill) throws ParseException {
        financialBillMapper.insert(financialBill);
        // checkout表
        getDataFromCheckIntoCheckOut(financialBill.getCheckinId());
        orderInfoService.updateOrderStatusById(financialBill.getCheckinId(), 2);
        roomInfoService.updateRoomStatusByRoomId(Integer.valueOf(financialBill.getRoomId()), 0);
        return CommonResult.successResponse(null);
    }

    public void getDataFromCheckIntoCheckOut(String checkinId) throws ParseException {
        // CheckinInfo checkinInfo = checkinInfoService.getById(checkinId);
        OrderInfo orderInfo = orderInfoService.getById(checkinId);
        QueryWrapper<ConsumptionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("checkin_id", checkinId);
        List<ConsumptionInfo> list = consumptionInfoService.list(wrapper);
        double totalConsumption  = 0.0;
        for (ConsumptionInfo info : list) {
            totalConsumption += info.getConsumptionAmout();
        }


        int daysBetween = DateUtils.daysBetween(orderInfo.getArrivedTime(), orderInfo.getDepartureTime());
        CheckoutInfo checkoutInfo = new CheckoutInfo();
        checkoutInfo.setCheckinId(orderInfo.getId());
        checkoutInfo.setRoomId(orderInfo.getRoomId());
        checkoutInfo.setRoomPrice(orderInfo.getRoomPrice());
        checkoutInfo.setDeposit(orderInfo.getDeposit());
        checkoutInfo.setCheckinDay(daysBetween);
        checkoutInfo.setActualReceive(orderInfo.getTotalPrice());
        checkoutInfo.setAccommodationExpenses(orderInfo.getTotalPrice());//住宿费用要计算
        checkoutInfo.setHotelConsumption(totalConsumption);//店内消费,附加商品查询
        checkoutInfo.setTelPrice(0.0);// 电话费
        checkoutInfo.setMealPrice(0.0);// 餐费
        checkoutInfo.setRefundableDeposit(orderInfo.getDeposit());
        checkoutInfo.setNetReceipts(orderInfo.getTotalPrice() + totalConsumption);
        checkoutInfo.setGiveChange(0.0);
        checkoutInfo.setOperator("前台");
        checkoutInfo.setRemarks("无");
        checkoutInfoMapper.insert(checkoutInfo);
    }
}
